﻿<%@ WebHandler Language="C#" Class="HandlerConfigPayment" %>

using System;
using System.Web;
using System.Web.SessionState;
using BLL;
using HighLowGameBLL;
using PaymentBLL;

public class HandlerConfigPayment : IHttpHandler, IReadOnlySessionState
{
    
    public void ProcessRequest (HttpContext context) {
         string type = context.Request.QueryString["t"] ?? string.Empty;

         switch (type)
         {
             case "GetItemIn":
                 GetItemIn(context);
                 break;
             case "GetItemInInfo":
                 GetItemInInfo(context);
                 break;
             case "GetItemOut":
                 GetItemOut(context);
                 break;
             case "GetItemOutInfo":
                 GetItemOutInfo(context);
                 break;
             case "GetPaymentGateWay":
                 GetPaymentGateWay(context);
                 break;
             case "SelectConvertAgToAu":
                 SelectConvertAgToAu(context);
                 break;
         }
    }
    private void GetItemIn(HttpContext context)
    {
        var ctr = new CtrItem();
        int paymentGateWayId=Convert.ToInt32(context.Request.QueryString["paymentGateWayId"]??"-1");
        int status=Convert.ToInt32(context.Request.QueryString["status"]??"-1");
        int currentPage=Convert.ToInt32(context.Request.QueryString["currentPage"]??"-1");
        int pageSize=Convert.ToInt32(context.Request.QueryString["pageSize"]??"-1");
        int? totalRecord = 0;
        var items = ctr.SelectItemInBy(paymentGateWayId, status, currentPage, pageSize, ref totalRecord);
        var strFunction = context.Request.UrlReferrer.AbsolutePath.Substring(context.Request.UrlReferrer.AbsolutePath.IndexOf("/") + 1).ToLower();

        var obj = new
                      {
                          Items = items,
                          TotalRecord = totalRecord,
                          Permission=CtrAdmin.GetPermission(strFunction)
                      };
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    private void GetItemInInfo(HttpContext context)
    {
        var ctr = new CtrItem();
        int id = Convert.ToInt32(context.Request.QueryString["id"] ?? "-1");

        var info = ctr.SelectItemIn(id);

        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(info);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    private void GetItemOut(HttpContext context)
    {
        var ctr = new CtrItem();
        int paymentGateWayId = Convert.ToInt32(context.Request.QueryString["paymentGateWayId"] ?? "-1");
        int status = Convert.ToInt32(context.Request.QueryString["status"] ?? "-1");
        int currentPage = Convert.ToInt32(context.Request.QueryString["currentPage"] ?? "-1");
        int pageSize = Convert.ToInt32(context.Request.QueryString["pageSize"] ?? "-1");
        int? totalRecord = 0;
        var items = ctr.SelectItemOutBy(paymentGateWayId, status, currentPage, pageSize, ref totalRecord);
        var strFunction = context.Request.UrlReferrer.AbsolutePath.Substring(context.Request.UrlReferrer.AbsolutePath.IndexOf("/") + 1).ToLower();

        var obj = new
        {
            Items = items,
            TotalRecord = totalRecord,
            Permission = CtrAdmin.GetPermission(strFunction)
        };
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    private void GetItemOutInfo(HttpContext context)
    {
        var ctr = new CtrItem();
        int id = Convert.ToInt32(context.Request.QueryString["id"] ?? "-1");

        var info = ctr.SelectItemOut(id);

        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(info);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }
    private void GetPaymentGateWay(HttpContext context)
    {
        var ctr = new CtrPaymentGateway();
        var gates = ctr.SelectAllPaymentGateway();
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(gates);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }
    private void SelectConvertAgToAu(HttpContext context)
    {
        var ctr = new CtrConvertAuAg();
        var cf = ctr.SelectConvertAgToAu();       
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(cf);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}